package cn.iocoder.yudao.module.system.service.erp.warehouse;
import cn.iocoder.yudao.module.system.controller.admin.erp.warehouse.vo.WarehousePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.erp.warehouse.vo.WarehouseSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.erp.warehouse.WarehouseDO;
import cn.iocoder.yudao.module.system.dal.mysql.erp.warehouse.WarehouseMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.WAREHOUSE_NOT_EXISTS;

/**
 * 仓库管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WarehouseServiceImpl implements WarehouseService {

    @Resource
    private WarehouseMapper warehouseMapper;

    @Override
    public Long createWarehouse(WarehouseSaveReqVO createReqVO) {
        // 插入
        WarehouseDO warehouse = BeanUtils.toBean(createReqVO, WarehouseDO.class);
        warehouseMapper.insert(warehouse);
        // 返回
        return warehouse.getId();
    }

    @Override
    public void updateWarehouse(WarehouseSaveReqVO updateReqVO) {
        // 校验存在
        validateWarehouseExists(updateReqVO.getId());
        // 更新
        WarehouseDO updateObj = BeanUtils.toBean(updateReqVO, WarehouseDO.class);
        warehouseMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarehouse(Long id) {
        // 校验存在
        validateWarehouseExists(id);
        // 删除
        warehouseMapper.deleteById(id);
    }
    @Override
    public void deleteWarehouse(List<Long>ids) {
        // 删除
        warehouseMapper.deleteByIds(ids);
    }

    private void validateWarehouseExists(Long id) {
        if (warehouseMapper.selectById(id) == null) {
            throw exception(WAREHOUSE_NOT_EXISTS);
        }
    }

    @Override
    public WarehouseDO getWarehouse(Long id) {
        return warehouseMapper.selectById(id);
    }

    @Override
    public WarehouseDO byNameGetWarehouse(String name) {
        return warehouseMapper.selectOne(WarehouseDO::getName,name);
    }

    @Override
    public PageResult<WarehouseDO> getWarehousePage(WarehousePageReqVO pageReqVO) {
        return warehouseMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WarehouseDO> getWarehouses(List<Long> ids) {
        return warehouseMapper.selectList(new LambdaQueryWrapper<WarehouseDO>().in(WarehouseDO::getId,ids));
    }

}